808C - Tea Party - CodeForces Solution


constructive algorithms greedy sortings *1400

Please click on ads to support us..

C++ Code:

//SHREE GANESHAYA NAMAH
//OM NAMAHA SHIVAY
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pb push_back
#define pob pop_back
#define mp make_pair
#define ff first
#define ss second
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(__null);
using namespace std;
// const int m=1e9+7;
// const int N=998244353;
// etf in sqrt(n) using prime factorisation
// ll int phi(ll int n){
//     ll int res=n;
//     for(ll int i=2;i*i<=n;i++){
//         if(n%i==0){
//             res/=i;
//             res*=(i-1);
//         }
//         while(n%i==0){
//             n/=i;
//         }
//     }
//     if(n>1){
//         res/=n;
//         res*=(n-1);
//     }
//     return res;
// }
// etf in nlog(logn) using sieve algo
// ll int p[1000001];
// void phif(ll int n){
//     for(ll int i=1;i<=n;i++){
//         p[i]=i;
//     }
//     for(ll int i=2;i<=n;i++){
//         if(p[i]==i){
//             for(ll int j=i;j<=n;j+=i){
//                 p[j]/=i;
//                 p[j]*=(i-1);
//             }
//         }
//     }
//     // for(ll int i=0;i<n;i++){
//     //     cout<<p[i]<<" ";
//     // }
// }
// vector<ll int>factorial(N+20);
// ll int bin(ll int a,ll int b,ll int m){
//     ll int ans=1;
//     while(b>0){
//         if(b&1){
//             ans=(ans*a)%m;
//         }
//         a=(a*a)%m;
//         b>>=1;
//     }
//     return ans%m;
// }
ll int cei(ll int a,ll int b){
    if(a%b==0){
        return a/b;
    }
    else{
        return (a/b)+1;
    }
}
// bool prime(ll int n){
//     for(ll int i=2;i*i<=n;i++){
//         if(n%i==0){
//             return false;
//         }
//     }
//     return true;
// }

 
int main(){
    fastio()
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ll int t=1;
    //cin>>t;
    while(t--){
        ll int n,w;
        cin>>n>>w;
        vector<pair<ll int,ll int>>v,ans;
        ll int s1=0,s2=0;
        for(ll int i=0;i<n;i++){
            ll int x;
            cin>>x;
            s1+=cei(x,2);
            s2+=x;
            v.pb({x,i});
        }
        
        if(w<s1 || w>s2){
            cout<<"-1";
        }
        else{
            ll int fl=0;
            sort(v.rbegin(),v.rend());
            for(ll int i=0;i<n;i++){
                ans.pb({cei(v[i].ff,2),v[i].ss});
                v[i].ff-=cei(v[i].ff,2);
            }
            w-=s1;
            ll int val=0;
            for(ll int i=0;i<n;i++){
                if(w==0){
                    fl=1;
                    break;
                }
                if(v[i].ff>=w){
                    val=w;
                    w=0;
                }
                else{
                    val=v[i].ff;
                    w-=v[i].ff;
                }
                ans[i].ff+=val;
            }
            vector<ll int>res(n);
            for(ll int i=0;i<n;i++){
                res[ans[i].ss]=ans[i].ff;
            }
            for(ll int i=0;i<n;i++){
                cout<<res[i]<<" ";
            }
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo